package listbyorder.access301_400.test341;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NestedIterator implements Iterator<Integer> {

    List<Integer> list = new ArrayList<>();
    int index;
    int len;

    public NestedIterator(List<NestedInteger> nestedList) {
        initList(nestedList);
        index = 0;
        len = list.size();
    }

    private void initList(List<NestedInteger> nestedList) {
        for (NestedInteger nestedInteger : nestedList) {
            if (nestedInteger.isInteger()) {
                list.add(nestedInteger.getInteger());
            } else {
                initList(nestedInteger.getList());
            }
        }
    }

    @Override
    public boolean hasNext() {
        return index < len;
    }

    @Override
    public Integer next() {
        return list.get(index++);
    }
}
